#!/bin/sh
#
# rabbitmq-server RabbitMQ broker
#
# chkconfig: - 80 05
# description: Manages RabbitMQ server
#

### BEGIN INIT INFO
# Provides:          rabbitmq-server
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description:       RabbitMQ broker
# Short-Description: Manages RabbitMQ server
### END INIT INFO

PATH=/sbin:/usr/sbin:/bin:/usr/bin
NAME=rabbitmq-server
DAEMON=/usr/sbin/${NAME}
CONTROL=/usr/sbin/rabbitmqctl
DESC="message broker"
USER=rabbitmq
PID_FILE=/var/run/rabbitmq/pid
RABBITMQ_STARTUP_TIMEOUT=600

test -x $DAEMON || exit 0
test -x $CONTROL || exit 0

RETVAL=0
set -e

[ -f /etc/default/${NAME} ] && . /etc/default/${NAME}

# $RABBITMQ_SERVER_CONSOLE_OUTPUT_DIR is a directory where rabbitmq-server(8)
# console output (both stdout and sterr) is redirected. It defaults to
# /var/log/rabbitmq which is configured by the package already (i.e. the
# directory is created and its ownership is set).
#
# We still look at the value of $RABBITMQ_LOG_BASE, possibly set in
# /etc/default/rabbitmq-server for backward compatibility. But we use a
# specific variable name here (instead of $RABBITMQ_LOG_BASE) to avoid any
# confusion with RabbitMQ's own logging configuration. Indeed, the console
# output redirection is a responsibility of the package, not RabbitMQ itself.
: ${RABBITMQ_SERVER_CONSOLE_OUTPUT_DIR:=${RABBITMQ_LOG_BASE:-/var/log/rabbitmq}}

. /lib/lsb/init-functions
. /lib/init/vars.sh

ensure_pid_dir () {
    PID_DIR=`dirname ${PID_FILE}`
    if [ ! -d ${PID_DIR} ] ; then
        mkdir -p ${PID_DIR}
        chown -R ${USER}:${USER} ${PID_DIR}
        chmod 755 ${PID_DIR}
    fi
}

remove_pid () {
    rm -f ${PID_FILE}
    rmdir `dirname ${PID_FILE}` || :
}

start_rabbitmq () {
    status_rabbitmq quiet
    if [ $RETVAL != 0 ] ; then
        RETVAL=0
        ensure_pid_dir
        set +e
        RABBITMQ_PID_FILE=$PID_FILE start-stop-daemon --quiet \
            --chuid rabbitmq --start \
            --pidfile "$PID_FILE" --background \
            --startas /bin/sh -- -c "exec $DAEMON >'${RABBITMQ_SERVER_CONSOLE_OUTPUT_DIR}/startup_log' 2>'${RABBITMQ_SERVER_CONSOLE_OUTPUT_DIR}/startup_err'"
        $CONTROL wait --timeout $RABBITMQ_STARTUP_TIMEOUT $PID_FILE >/dev/null 2>&1
        RETVAL=$?
        set -e
        if [ $RETVAL != 0 ] ; then
            remove_pid
        fi
    else
        RETVAL=3
    fi
}

stop_rabbitmq () {
    status_rabbitmq quiet
    if [ $RETVAL = 0 ] ; then
        set +e
        $CONTROL stop ${PID_FILE} \
            > ${RABBITMQ_SERVER_CONSOLE_OUTPUT_DIR}/shutdown_log \
            2> ${RABBITMQ_SERVER_CONSOLE_OUTPUT_DIR}/shutdown_err
        RETVAL=$?
        set -e
        if [ $RETVAL = 0 ] ; then
            remove_pid
        fi
    else
        RETVAL=3
    fi
}

status_rabbitmq() {
    set +e
    if [ "$1" != "quiet" ] ; then
        $CONTROL status 2>&1
    else
        $CONTROL status > /dev/null 2>&1
    fi
    if [ $? != 0 ] ; then
        RETVAL=3
    fi
    set -e
}

rotate_logs_rabbitmq() {
    set +e
    $CONTROL -q rotate_logs
    if [ $? != 0 ] ; then
        RETVAL=1
    fi
    set -e
}

restart_running_rabbitmq () {
    status_rabbitmq quiet
    if [ $RETVAL = 0 ] ; then
        restart_rabbitmq
    else
        log_warning_msg "${DESC} not running"
    fi
}

restart_rabbitmq() {
    stop_rabbitmq
    start_rabbitmq
}

restart_end() {
    if [ $RETVAL = 0 ] ; then
        log_end_msg 0
    else
        log_end_msg 1
    fi
}

start_stop_end() {
    case "$RETVAL" in
        0)
            [ -x /sbin/initctl ] && /sbin/initctl emit --no-wait "${NAME}-${1}"
            log_end_msg 0
            ;;
        3)
            log_warning_msg "${DESC} already ${1}"
            log_end_msg 0
            RETVAL=0
            ;;
        *)
            log_warning_msg "FAILED - check ${RABBITMQ_SERVER_CONSOLE_OUTPUT_DIR}/startup_\{log, _err\}"
            log_end_msg 1
            ;;
    esac
}

case "$1" in
    start)
        log_daemon_msg "Starting ${DESC}" $NAME
        start_rabbitmq
        start_stop_end "running"
        ;;
    stop)
        log_daemon_msg "Stopping ${DESC}" $NAME
        stop_rabbitmq
        start_stop_end "stopped"
        ;;
    status)
        status_rabbitmq
        ;;
    rotate-logs)
        log_action_begin_msg "Rotating log files for ${DESC}: ${NAME}"
        rotate_logs_rabbitmq
        log_action_end_msg $RETVAL
        ;;
    force-reload|reload|restart)
        log_daemon_msg "Restarting ${DESC}" $NAME
        restart_rabbitmq
        restart_end
        ;;
    try-restart)
        log_daemon_msg "Restarting ${DESC}" $NAME
        restart_running_rabbitmq
        restart_end
        ;;
    *)
        echo "Usage: $0 {start|stop|status|rotate-logs|restart|condrestart|try-restart|reload|force-reload}" >&2
        RETVAL=1
        ;;
esac

exit $RETVAL
